﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using CoCoonCoreWM6;
using System.Diagnostics;

namespace CoCoonWM6
{
    public partial class CoCoon : Form
    {
        CoCo coco;
        Display display; 

        public CoCoon()
        {
            InitializeComponent();
        }

        private void CoCoon_Load(object sender, EventArgs e)
        {
            coco = new CoCo(64*1024);

            display = new Display(coco);

            //Thread cocoThread = new Thread(coco.Run);

            //cocoThread.Start();

            //TODO: now what? 

        }

        private void CoCoon_Paint(object sender, PaintEventArgs e)
        {
            pbxScreen.BackColor = Color.Black;
            display.Draw(pbxScreen); 
            pbxScreen.Update();

            //Thread.Sleep(20); // 50 Hz
            coco.Run(20);//Assume 50 Hz

            Invalidate();
        }

        private void CoCoon_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine(e.KeyData + ": " + e.KeyValue);

            coco.KeyboardInput(e.KeyValue);
        }

        private void CoCoon_KeyPress(object sender, KeyPressEventArgs e)
        {
            //clear input
            coco.KeyboardInput(null);
        }

        private void CoCoon_Closed(object sender, EventArgs e)
        {
            coco.TearDown();
        }

        //protected override void OnPaintBackground(PaintEventArgs e)
        //{
        //    // Do nothing
        //}
    }
}